import { keyCodeDt, PlayerDt } from "../../../Framework/Common/CommonInterface";
import Emitter from "../../../Framework/Common/Emitter";
import { keyCodeMgr, playerDtMgr } from "../../../Framework/Data/DataBaseMgr";
import DataMgr from "../../../Framework/Data/DataMgr";
import MainUI from "../../UI/MainUI";
import DeepClone from "../../Utils/DeepClone";
import VecUtils from "../../Utils/VecUtils";
import BubbleLayer from "../Module/BubbleLayer";
import ModuleMgr from "../Module/ModuleMgr";
import PlayerLayer from "../Module/PlayerLayer";
import TiledMapCtrl from "../Module/TiledMapCtrl";
import MoveCtrl from "./Control/MoveCtrl";
import StateCtrl from "./Control/StateCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Player extends cc.Component {
    //存储这个玩家的速度等数据
    private _playerDt: PlayerDt = null;
    get playerDt() {
        return this._playerDt;
    }
    //表示当前是第几个玩家
    private _playerIndex: number = null;
    set playerIndex(value: number) {
        this._playerIndex = value;
    }
    //角色数据 0,1,2,3
    private _roleIndex: number = null;
    set roleIndex(value: number) {
        this._roleIndex = value;
    }
    get roleIndex() {
        return this._roleIndex;
    }
    //存储键盘数据
    private _normalKeyCode = null;
    //获取翻转数据
    private _reverseKeyCode = null;
    //玩家的速度
    private _speed: number = 0;
    get speed() {
        return this._speed;
    }
    //玩家的移动方向
    private _dir: cc.Vec2 = cc.v2();
    //存储上一次的方向
    private _preDir: cc.Vec2 = cc.v2();
    //PlayerLayer脚本
    private _playerLayer: PlayerLayer = null;
    //移动控制
    private _moveCtrl: MoveCtrl = null;
    //状态控制
    private _StateCtrl: StateCtrl = null;
    get statsCtrl() {
        return this._StateCtrl;
    }

    set playerLayer(playerLayerTs) {
        this._playerLayer = playerLayerTs;
    }

    private _tiledMapCtrl: TiledMapCtrl = null;
    //玩家的格子坐标
    private _playerTiledPos = cc.v2();
    get playerTiledPos() {
        return this._playerTiledPos;
    }
    set playerTiledPos(tiledPos: cc.Vec2) {
        this._playerTiledPos = tiledPos;
    }
    private _objDir = {
        runUp: cc.v2(0, 1),
        runDown: cc.v2(0, -1),
        runRight: cc.v2(1, 0),
        runLeft: cc.v2(-1, 0),
    }
    //获取泡泡层
    private _bubbleLayer: BubbleLayer = null;
    private _keyCodeDt: keyCodeDt = null;
    //此玩家已死亡
    private _isDeath: boolean = false;
    get isDeath() {
        return this._isDeath;
    }
    set isDeath(isdeath: boolean) {
        this._isDeath = isdeath;
    }
    private _AniComp: cc.Animation = null;
    oninit() {
        //获取玩家的数据
        this._playerDt = DeepClone.deepClone(DataMgr.instance.getMgr<playerDtMgr>("playerDtMgr").getDataByID(2001 + this._roleIndex)) as PlayerDt;
        let keyCodeMgr = DataMgr.instance.getMgr<keyCodeMgr>("keyCodeMgr");
        this._keyCodeDt = DeepClone.deepClone(keyCodeMgr.getDataByID(1001 + this._playerIndex - 1)) as keyCodeDt;
        this._normalKeyCode = this._keyCodeDt.dir;
        this._reverseKeyCode = this._keyCodeDt.reverseDir;
        this._addController();
        this._tiledMapCtrl = ModuleMgr.instance.getModule<TiledMapCtrl>("TiledMapCtrl");
        this._bubbleLayer = ModuleMgr.instance.getModule<BubbleLayer>("BubbleLayer");
        this._preDir = cc.v2(0, -1);
        this._dir = cc.v2(0, -1);
        this._AniComp = this.getComponent(cc.Animation);
    }

    private _addController() {
        this._moveCtrl = new MoveCtrl(this);
        this._StateCtrl = new StateCtrl(this);
    }

    onUpdate(dt: number, ...rest: any[]) {
        this._moveCtrl.onUpdate.apply(this._moveCtrl, arguments);
        //this.checkPlayerPosition();
        //设置玩家的层级
        this.setPlayerLayer();
    }

    onKeyDown(keyCode: number) {
        let dirString = this._normalKeyCode[keyCode];
        if (!dirString) {
            return;
        }
        if (dirString == "addBubble") {
            //释放泡泡
            Emitter.instance.emit("AddBubble", VecUtils.getVec2(this.node.position), this);
        }
        this._dir = this._objDir[dirString];
        this._preDir = this._dir;
        if (!this._dir) {
            return;
        }
        this._moveCtrl.dir = this._dir;
        this._speed = this._playerDt.speed;
        this._StateCtrl.changeState(dirString);
    }
    onKeyUp(keyCode: number) {
        let dirString = this._normalKeyCode[keyCode];
        //可以实现玩家一边移动一边放泡泡
        if (!dirString || dirString == "addBubble") {
            return;
        }
        this._StateCtrl.changeState("Idle");
        this._dir = cc.v2();
        this._moveCtrl.dir = this._dir;
        this._speed = 0;
    }

    //设置玩家的层级
    setPlayerLayer() {
        //获取玩家所在的层级
        let layer = (this._tiledMapCtrl.getTiledPosByPoxiPos(VecUtils.getVec2(this.node.position)).y);
        this.node.setSiblingIndex(layer);
        this.node.zIndex = layer + 1;
    }

    //玩家的数据进行翻转
    reverse(value: number) {
        this._normalKeyCode = this._reverseKeyCode;
        this.scheduleOnce(() => {
            this._normalKeyCode = this._keyCodeDt.dir;
        }, value);
    }
    //玩家死亡后的帧事件
    playerDie() {
        this.node.opacity = 255;
        cc.tween(this.node)
            .repeatForever(cc.tween()
                .to(0.5, { opacity: 0 })
                .to(0.5, { opacity: 255 }))
            .start();
        let MainUINode = cc.find("Canvas/UIRoot/Normal/MainUI");
        let mainUIComp = MainUINode.getComponent(MainUI);
        mainUIComp.openGameOver(3 - this.playerIndex);

    }
}